package com.fbty.mt.action.admin;

import com.fbty.base.action.BaseAction;
import com.fbty.mt.service.IpService;
import com.fbty.mt.utils.SingletonThreadPool;
import com.fbty.mt.utils.StaticValue;

public class IPThreadAction extends BaseAction {

	private int total;
	
	private int errorTotal;
	
	private boolean flag;
	
	private IpService ipInfoService;
	
	public String execute(){
		int i = (Integer)(sessionMap.get("infoNum")==null?0:sessionMap.get("infoNum"));
		int t = (Integer)sessionMap.get("totalLine");
		total = StaticValue.uploadTxt.size();
		errorTotal = StaticValue.errrorIp.size();
		//全部ip都获取完后，回收已经使用完的线程
		if(total==0){
			//SingletonThreadPool.getInstance().getPool().shutdown();
			if(StaticValue.ipInfoes.size()>0){
				//保存完当前集合里面的ip记录
				synchronized (StaticValue.obj) {
					ipInfoService.saveIpInfos(StaticValue.ipInfoes);
					sessionMap.put("infoNum", i+StaticValue.ipInfoes.size());
					StaticValue.ipInfoes.clear();
				}
			}
		}
		//当有10记录时候保存一次数据库
		if(StaticValue.ipInfoes.size()==30){
			synchronized (StaticValue.obj) {
				ipInfoService.saveIpInfos(StaticValue.ipInfoes);
				StaticValue.ipInfoes.clear();
				sessionMap.put("infoNum",i+30);
			}
		}
		//当上传的ip都解析完后，将返回json到前端，停止继续获取后台信息
		if(total==0&&(i+errorTotal)==t){
			flag=true;
			sessionMap.put("infoNum",0);
		}
		return "success";
	}


	public String onlyMsg(){
		int i = (Integer)(sessionMap.get("onlyinfoNum")==null?0:sessionMap.get("onlyinfoNum"));
		int t = (Integer)sessionMap.get("totalOnlyLine");
		total = StaticValue.uploadOnlyTxt.size();
		errorTotal = StaticValue.errorOnlyTxt.size();
		//全部ip都获取完后，回收已经使用完的线程
		if(total==0){
			//SingletonThreadPool.getInstance().getPool().shutdown();
			if(StaticValue.uploadOnlyIps.size()>0){
				//保存完当前集合里面的ip记录
				ipInfoService.saveOnlyIpInfos(StaticValue.uploadOnlyIps);
				sessionMap.put("onlyinfoNum", i+StaticValue.uploadOnlyIps.size());
				StaticValue.uploadOnlyIps.clear();
			}
		}
		//当有10记录时候保存一次数据库
		if(StaticValue.uploadOnlyIps.size()==10){
			ipInfoService.saveOnlyIpInfos(StaticValue.uploadOnlyIps);
			StaticValue.uploadOnlyIps.clear();
			sessionMap.put("onlyinfoNum",i+10);
		}
		//当上传的ip都解析完后，将返回json到前端，停止继续获取后台信息
		if(total==0&&(i+errorTotal)==t){
			flag=true;
			sessionMap.put("onlyinfoNum",0);
		}
		return "success";
	}
	
	public int getTotal() {
		return total;
	}


	public int getErrorTotal() {
		return errorTotal;
	}


	public boolean isFlag() {
		return flag;
	}


	public void setIpInfoService(IpService ipInfoService) {
		this.ipInfoService = ipInfoService;
	}
	
	
	
}
